package com.mairuide.bill.treeview;

import com.mairuide._frame.config.CacheableConfig;
import com.mairuide._frame.utils.AccountNode;
import com.mairuide.casemanage.service.CaseManageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly=true)
public class BillTreeService {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	CaseManageService caseManageService;
	
	/**
	 * 获取案件下面的人员卡号等信息
	 * @param caseId
	 * @return
	 */
	@Cacheable(value= CacheableConfig.BILL_SHOUYE,key="#root.methodName+#p0")
	public List<AccountNode> getAccountTree(String caseId){
		String whichTable = caseManageService.getWhichTable(caseId);
		List<AccountNode> nodes = new ArrayList<AccountNode>();//把所有资源转换成树模型的节点集合，此容器用于保存所有节点
		AccountNode rootNode = new AccountNode();
		rootNode.setNodeId("0");
		rootNode.setText("交易主体名单");
		nodes.add(rootNode);
		
		StringBuffer sb = new StringBuffer();
		sb.append("select distinct ACCOUNT_BODY,ACCOUNT_BODY_CARD from "+whichTable+" where IS_DELETE = 0 ");
		if(StringUtils.isNotEmpty(caseId)) {
			sb.append(" and casemanage_id ='" + caseId + "' ");
		}
		sb.append(" order by account_body asc");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
		if(list!=null && list.size()>0) {
			List<AccountNode> nodes2 = new ArrayList<AccountNode>();//把所有资源转换成树模型的节点集合，此容器用于保存所有节点
			Map<String, List<String>> accountMap = new HashMap<String, List<String>>();
			int unknow=1;
			for(Map<String, Object> account : list) {
				String accountBody = account.get("ACCOUNT_BODY")==null ? "" : account.get("ACCOUNT_BODY").toString();
				String accountBodyCard = account.get("ACCOUNT_BODY_CARD")==null ? "" : account.get("ACCOUNT_BODY_CARD").toString();
				
				if(StringUtils.isEmpty(accountBody)) {
					accountBody = "未知" + unknow;
					unknow++;
				}
				List<String> accountCards = new ArrayList<String>();
				if(accountMap.get(accountBody)!=null) {
					accountCards = accountMap.get(accountBody);
				}
				if(StringUtils.isNotBlank(accountBodyCard)) accountCards.add(accountBodyCard);
				
				accountMap.put(accountBody, accountCards);
			}
			
			int i=1;
			for (String accountBody : accountMap.keySet()) { 
				AccountNode node = new AccountNode();
				node.setNodeId(i+"");
				node.setText(accountBody);
				node.setAccountBody(accountBody);
				node.getState().setExpanded(true);
				node.setSelectable(false);
				node.getState().setSelecttable(true);
				node.setShowEditDel(true);
				i++;
				
				List<String> accountBodys = accountMap.get(accountBody);
				if(accountBodys!=null && accountBodys.size()>0) {
					List<AccountNode> childs = new ArrayList<AccountNode>();
					for(int j=0;j<accountBodys.size();j++) {
						int n = j+1;
						String accountBodyCard = accountBodys.get(j).toString();
						AccountNode child = new AccountNode();
						child.setNodeId(i+"_"+n);
						child.setText(accountBodyCard);
						child.setAccountBody(accountBody);
						child.setAccountBodyCard(accountBodyCard);
						child.getState().setExpanded(true);
						child.setSelectable(true);
						child.setNodes(null);
						child.getState().setSelecttable(true);
						childs.add(child);
					}
					node.setNodes(childs);
				}
				nodes2.add(node);
			}
			rootNode.setNodes(nodes2);
		}
		return nodes;
	}
	
	/**
	 * 根据案件id查询交易主题数量
	 * @param caseManageId
	 * @return
	 */
	@Cacheable(value= CacheableConfig.BILL_SHOUYE,key="'getAccountNumberByCaseManageId'+#caseManageId")
	public Map<String,Object> getAccountNumberByCaseManageId(String caseManageId){
		String whichTable = caseManageService.getWhichTable(caseManageId);
		StringBuffer sbfSql = new StringBuffer();
		sbfSql.append("select count(1) as ACCOUNTBYDONUM from (");
		sbfSql.append("select distinct (case when account_body is not null then account_body else account_body_card end) as name from "+whichTable+" t ");
		sbfSql.append("where t.is_delete =0 ");
		if(com.mairuide._frame.utils.StringUtils.isNotEmpty(caseManageId)) {
			sbfSql.append("and t.casemanage_id=");
			sbfSql.append("'"+caseManageId+"'");
		}
		sbfSql.append(") t2");
		
		Map<String, Object> list = jdbcTemplate.queryForMap(sbfSql.toString());
		return list;
	}
	
	/**
	 * 获取案件下面的人员卡号等信息
	 * @param caseId
	 * @return
	 */
	public List<BillTreeNode> getAccountTree2(String caseId){
		String whichTable = caseManageService.getWhichTable(caseId);
		List<BillTreeNode> nodes = new ArrayList<BillTreeNode>();//把所有资源转换成树模型的节点集合，此容器用于保存所有节点
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT MAX(ACCOUNT_BODY) AS ACCOUNT_BODY, ACCOUNT_BODY_CARD FROM "+whichTable+" where IS_DELETE = 0 ");
		if(StringUtils.isNotEmpty(caseId)) {
			sb.append(" and casemanage_id ='" + caseId + "' and ACCOUNT_BODY_CARD is not null ");
		}
		sb.append(" group by ACCOUNT_BODY_CARD order by account_body asc");
		List<Map<String, Object>> list = jdbcTemplate.queryForList(sb.toString());
		if(list!=null && list.size()>0) {
			Map<String, List<String>> accountMap = new HashMap<String, List<String>>();
			int unknow=1;
			for(Map<String, Object> account : list) {
				String accountBody = account.get("ACCOUNT_BODY")==null ? "" : account.get("ACCOUNT_BODY").toString();
				String accountBodyCard = account.get("ACCOUNT_BODY_CARD")==null ? "" : account.get("ACCOUNT_BODY_CARD").toString();
				
				List<String> accountCards = new ArrayList<String>();
				if(StringUtils.isEmpty(accountBody)) {
					if(accountCards.contains(accountBodyCard)) {
						continue;
					}
					accountBody = "未知" + unknow;
					unknow++;
				}
				if(accountMap.get(accountBody)!=null) {
					accountCards = accountMap.get(accountBody);
				}
				if(StringUtils.isNotBlank(accountBodyCard)) accountCards.add(accountBodyCard);
				
				accountMap.put(accountBody, accountCards);
			}
			int i=1;
			for (String accountBody : accountMap.keySet()) {
				BillTreeNode treeNode = new BillTreeNode();
				treeNode.setId(i+"");
				treeNode.setName(accountBody);
				treeNode.setTitle(accountBody);
				treeNode.setPId("pid_0");
				treeNode.setOpen(false);
				treeNode.setIsParent(true);
	        	nodes.add(treeNode);
				
				List<String> accountBodys = accountMap.get(accountBody);
				if(accountBodys!=null && accountBodys.size()>0) {
					for(int j=0;j<accountBodys.size();j++) {
						int n = j+1;
						String accountBodyCard = accountBodys.get(j).toString();
						BillTreeNode treeNode2 = new BillTreeNode();
						treeNode2.setId(i+"_"+n);
						treeNode2.setName(accountBodyCard);
						treeNode2.setAccountBody(accountBody);
						treeNode2.setAccountBodyCard(accountBodyCard);
						treeNode2.setTitle(accountBodyCard);
						treeNode2.setPId(i+"");
						treeNode2.setOpen(false);
						treeNode2.setIsParent(false);
			        	nodes.add(treeNode2);
					}
				}
				i++;
			}
		}
		return nodes;
	}
}
